package com.swust.exam.controller;



import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.swust.exam.pojo.Classes;
import com.swust.exam.pojo.Course;
import com.swust.exam.pojo.Questionbank;
import com.swust.exam.pojo.Students;
import com.swust.exam.pojo.Teachers;
import com.swust.exam.service.ClassesService;
import com.swust.exam.service.CourseService;
import com.swust.exam.service.QuestionService;
import com.swust.exam.service.QuestionbankService;
import com.swust.exam.service.StudentsService;
import com.swust.exam.service.TeachersService;

@Controller
public class UserController {
	@RequestMapping(value = "/")
	public String begin(Model model)
	{	
		return "index";
	}
	
	
	@RequestMapping(value = "/introduce")
	public String introduce(Model model)
	{	
		return "introduce";
	}
	
	@Autowired
	private ClassesService classesService;
	@RequestMapping(value = "/classes")
	public String classes(HttpSession session,Model model)
	{	
		/*Students s = (Students)session.getAttribute("student");
		Teachers c =  (Teachers)session.getAttribute("teacher");
		if(s==null&&c==null)
			return "/loginUser";*/
		List<Classes> list=classesService.classeslist();
		model.addAttribute("classeslist",list);
		List<Classes> list2=classesService.courselist();
		 model.addAttribute("courselist",list2);
		 List<Classes> list3=classesService.teacherslist();
		 model.addAttribute("teacherslist",list3);
		return "classes";
	}
	
	@Autowired
	private CourseService courseservice;
	@Autowired ClassesService classesservice;
	@Autowired StudentsService studentsservice;
	@RequestMapping(value = "/student")
	public String student(HttpSession session,Model model){	
		List<Course> courselist = courseservice.courselist();
		model.addAttribute("courselist",courselist);
		List<Classes> classlist=classesservice.GetDetailClasses();
		model.addAttribute("classlist",classlist);
		List<Classes> CourseNamelist=classesservice.GetCourseName();
		model.addAttribute("CourseNamelist",CourseNamelist);
		List<Students> studentslist=studentsservice.GetDetailStudentInfo();
		model.addAttribute("studentslist",studentslist);
		return "students";
	}
	
	@Autowired
	private CourseService courseService;
	
	@RequestMapping(value = "/course")
	public String course(HttpSession session,Model model)
	{	
		List<Course> list=courseService.courselist();
		model.addAttribute("courselist",list);
		return "course";
	}
	
	@Resource
	private TeachersService teachersService;
	@RequestMapping(value = "/teacher")
	public String teacher(HttpSession session,Model model){
		List<Teachers> list=teachersService.teacherlist();
		model.addAttribute("teacherlist", list);
		return "teachers";
	}
	
	
	@Autowired
	private QuestionbankService questionbankService;
	
	@RequestMapping(value = "/question")
	public String question(HttpSession session,Model model)
	{	 
		List<Questionbank> list=questionbankService.questionbanklist();
		model.addAttribute("questionbanklist",list);
		return "question";
	}
	
	@RequestMapping(value = "/exam")
	public String exam(HttpSession session,Model model)
	{
		return "exam";
	}
	
	@RequestMapping(value="/student_login",method = RequestMethod.POST)
	public ModelAndView student_login(Students student,HttpSession session){
		String StudentNumber=student.getStudentNumber();
		String studentPassword=student.getStudentPassword();
		encrypt md=new encrypt();
		String password =md.convert(studentPassword);
		Students b=studentsservice.login(StudentNumber, password);
		ModelAndView mav;
		if (b == null) {
			mav=new ModelAndView("loginUser");
			mav.addObject("error", "锟矫伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较拷锟�");
			return mav;
		} else {
			session.setAttribute("student", b);
			mav=new ModelAndView("introduce");
			return mav;
		} 
	}
	
	@RequestMapping(value="/teacher_login",method=RequestMethod.POST)
	public ModelAndView teacher_login(Teachers teacher,HttpSession session){
		String teacherNumber=teacher.getTeacherNumber();
		String teacherPassword=teacher.getTeacherPassword();
		encrypt md5=new encrypt();
		String password=md5.convert(teacherPassword);
		Teachers a=teachersService.login(teacherNumber,password);
		ModelAndView mav;
		if(a == null){
			mav=new ModelAndView("loginUser");
			return mav;
		}else{
				session.setAttribute("teacher", a);
			    mav=new ModelAndView("introduce");
			    return mav;
			}
		}
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session,Model model){
		session.removeAttribute("student");
		session.removeAttribute("teacher");
		return "introduce";
	}
	
	@RequestMapping(value = "/loginUser")
	public String loginUser(Model model)
	{
		return "loginUser";
	}
	
	@RequestMapping(value = "/view")
	public String view(Model model){
		
		return "view";
	}
	
	@Autowired
	private QuestionService questionService;
	@RequestMapping(value = "/look")
	public String look(int id,Model model){	
		String ContentStr=questionService.get(id).getContentStr();
		model.addAttribute("content",ContentStr);		
		return "look";
	}
	@RequestMapping(value="/update")
	public String update(Model model,HttpSession session){
		Students s = (Students)session.getAttribute("student");
		Teachers c =  (Teachers)session.getAttribute("teacher");
		if(s==null&&c==null)
			return "loginUser";
		return "update";
	}
	@RequestMapping(value="/update/password",method=RequestMethod.POST)
	public String passwordupdate(Students student,Teachers teacher,Model model,HttpSession session){
		encrypt md5=new encrypt();
		Students s =  (Students)session.getAttribute("student");
		if(s!=null){
			String sp=student.getStudentPassword();
			sp=md5.convert(sp);
			String sn=s.getStudentNumber();
			studentsservice.password_update(sp, sn);
		}
		Teachers c =  (Teachers)session.getAttribute("teacher");
		if(c!=null&&s==null){
        String cp=teacher.getTeacherPassword();
        cp=md5.convert(cp);
		String cn=c.getTeacherNumber();
			teachersService.password_update(cp,cn);
		}
		return "/logout";
		}
}
